/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Bussiness.DepartmentDAO;
import Bussiness.LocationDAO;
import Bussiness.ProjectDAO;
import Bussiness.TransferDAO;
import Connection.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author SONY
 */
public class Allowance extends javax.swing.JFrame {
    DefaultTableModel model;
    String currentProject;
    String toProject;
    String currentDepartment;
    String toDepartment;
    String currentLocation;
    String toLocation;
    
    int allowanceProject = 0;
    int allowanceDepartment = 0;
    int allowanceLocation = 0;
    int total = 0;
    int count = 0;
    /**
     * Creates new form Allowance
     */
    public Allowance() {
        initComponents();
        loadData();
        try {
            // TODO add your handling code here:
            int count = totalAllowance();
            txtTotalAllowance.setText(String.valueOf(count));
        } catch (SQLException ex) {
            Logger.getLogger(Allowance.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tbBang = new javax.swing.JTable();
        txtAllowance = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        txtTransferID = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        txtEmployeeID = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtTotalAllowance = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel6 = new javax.swing.JLabel();
        jSeparator3 = new javax.swing.JSeparator();
        jLabel7 = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        tbBang.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT", "Transfer ID", "Employee ID", "From Project", "To Project", "From Department", "To Department", "From Location", "To Location", "Join Date", "End Date", "Status"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Boolean.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tbBang.setGridColor(new java.awt.Color(255, 204, 204));
        tbBang.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbBangMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbBang);

        txtAllowance.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(255, 0, 0));
        jLabel3.setText("Total Allowance");

        jLabel4.setForeground(new java.awt.Color(0, 102, 0));
        jLabel4.setText("Employee Transfer Management");

        jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 20)); // NOI18N
        jLabel5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/sendrequest.png"))); // NOI18N
        jLabel5.setText("ALLOWANCE");

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/16x16/search.png"))); // NOI18N
        jButton1.setText("Search");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Transfer ID");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Employee ID");

        txtTotalAllowance.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(0, 102, 0));
        jLabel6.setText("Employee Allowance");

        jLabel7.setText("(VND)");

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icons/16x16/accept.png"))); // NOI18N
        jButton2.setText("OK");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(39, 39, 39)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtEmployeeID, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                    .addComponent(txtTransferID))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addComponent(jLabel5)
                .addGap(117, 117, 117))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator1)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(18, 18, 18)
                        .addComponent(txtAllowance, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addGap(32, 32, 32)
                        .addComponent(txtTotalAllowance, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jLabel4)
                        .addGap(12, 12, 12))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 252, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel4))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtTransferID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton1)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtEmployeeID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))))
                .addGap(18, 18, 18)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtTotalAllowance, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtAllowance, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addComponent(jButton2)
                .addContainerGap(91, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tbBangMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbBangMouseClicked
        // TODO add your handling code here:
        currentProject = tbBang.getValueAt(tbBang.getSelectedRow(), 3).toString();
        currentDepartment = tbBang.getValueAt(tbBang.getSelectedRow(), 5).toString();
        currentLocation = tbBang.getValueAt(tbBang.getSelectedRow(), 7).toString();
        toProject = tbBang.getValueAt(tbBang.getSelectedRow(), 4).toString();
        toDepartment = tbBang.getValueAt(tbBang.getSelectedRow(), 6).toString();
        toLocation = tbBang.getValueAt(tbBang.getSelectedRow(), 8).toString();
        
        if (!currentProject.equals(toProject)) {
            try {
                allowanceProject = ProjectDAO.getAllowance(toProject);
            } catch (SQLException ex) {
                Logger.getLogger(Allowance.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            allowanceProject = 0;
        }
        
        if (!currentDepartment.equals(toDepartment)) {
            try {
                allowanceDepartment = DepartmentDAO.getAllowance(toDepartment);
            } catch (SQLException ex) {
                Logger.getLogger(Allowance.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        } else{
            allowanceDepartment = 0;
        }
        
        if (!currentLocation.equals(toLocation)) {
            try {
                allowanceLocation = LocationDAO.getAllowance(toLocation);
            } catch (SQLException ex) {
                Logger.getLogger(Allowance.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else{
            allowanceLocation = 0;
        }
        total = allowanceProject + allowanceDepartment + allowanceLocation;
        txtAllowance.setText(String.valueOf(total));
    }//GEN-LAST:event_tbBangMouseClicked

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        txtAllowance.setText("");
        if (txtTransferID.getText().equals("")) {
            if (txtEmployeeID.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "Please Enter For Key Word Search");
            } else{
                String search = txtEmployeeID.getText();
                loadDataSearchEmployeeID(search);
            }
        } else{
            String search = txtTransferID.getText();
            loadDataSearch(search);
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    public int totalAllowance() throws SQLException{
        count = TransferDAO.countTotalTransfer();
        DBUtil db = new DBUtil();
        Connection conn = db.connect();
        String SQL = "SELECT * FROM TRANSFER";
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(SQL);
        int total1 = 0;
        while (rs.next()) {
            String fromProject = rs.getString(4);
            String fromDepartment = rs.getString(6);
            String fromLocation = rs.getString(8);
            String toProject1 = rs.getString(5);
            String toDepartment1 = rs.getString(7);
            String toLocation1 = rs.getString(9);
            //System.out.println(fromProject);
            if (!fromProject.equals(toProject1)) {
                try {
                    allowanceProject = ProjectDAO.getAllowance(toProject1);
                } catch (SQLException ex) {
                    Logger.getLogger(Allowance.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                allowanceProject = 0;
            }
        
            if (!fromDepartment.equals(toDepartment1)) {
                try {
                    allowanceDepartment = DepartmentDAO.getAllowance(toDepartment1);
                } catch (SQLException ex) {
                    Logger.getLogger(Allowance.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else{
                allowanceDepartment = 0;
            }
        
            if (!fromLocation.equals(toLocation1)) {
                try {
                    allowanceLocation = LocationDAO.getAllowance(toLocation1);
                } catch (SQLException ex) {
                    Logger.getLogger(Allowance.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else{
                allowanceLocation = 0;
            }
            total = allowanceProject + allowanceDepartment + allowanceLocation;
            total1 = total1 + total;
        }
        return total1;
    }
    
    public void loadData(){
        DBUtil db = new DBUtil();
        Connection conn = db.connect();
        String SQL = "SELECT * FROM TRANSFER";
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(SQL);
            model = (DefaultTableModel)tbBang.getModel();
            while (rs.next()) {  
                Vector v = new Vector();
                v.add(rs.getString(1));
                v.add(rs.getString(2));
                v.add(rs.getString(3));
                v.add(rs.getString(4));
                v.add(rs.getString(5));
                v.add(rs.getString(6));
                v.add(rs.getString(7));
                v.add(rs.getString(8));
                v.add(rs.getString(9));
                v.add(rs.getString(10));
                v.add(rs.getString(11));
                v.add(rs.getBoolean(12));
                model.addRow(v);
            }
            conn.close();
            //rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(Allowance.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public void loadDataSearch(String transferSearch){
        DBUtil db = new DBUtil();
        Connection conn = db.connect();
        String SQL = "SELECT * FROM TRANSFER WHERE TRANSFERID ='" + transferSearch + "'";
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(SQL);
            model = (DefaultTableModel)tbBang.getModel();
            while (model.getRowCount()>0) {                
                model.removeRow(0);
            }
            while (rs.next()) {  
                Vector v = new Vector();
                v.add(rs.getString(1));
                v.add(rs.getString(2));
                v.add(rs.getString(3));
                v.add(rs.getString(4));
                v.add(rs.getString(5));
                v.add(rs.getString(6));
                v.add(rs.getString(7));
                v.add(rs.getString(8));
                v.add(rs.getString(9));
                v.add(rs.getString(10));
                v.add(rs.getString(11));
                v.add(rs.getBoolean(12));
                model.addRow(v);
            }
            conn.close();
            //rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(Allowance.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public void loadDataSearchEmployeeID(String employeeIDSearch){
        DBUtil db = new DBUtil();
        Connection conn = db.connect();
        String SQL = "SELECT * FROM TRANSFER WHERE EMPLOYEEID ='" + employeeIDSearch + "'";
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(SQL);
            model = (DefaultTableModel)tbBang.getModel();
            while (model.getRowCount()>0) {                
                model.removeRow(0);
            }
            while (rs.next()) {  
                Vector v = new Vector();
                v.add(rs.getString(1));
                v.add(rs.getString(2));
                v.add(rs.getString(3));
                v.add(rs.getString(4));
                v.add(rs.getString(5));
                v.add(rs.getString(6));
                v.add(rs.getString(7));
                v.add(rs.getString(8));
                v.add(rs.getString(9));
                v.add(rs.getString(10));
                v.add(rs.getString(11));
                v.add(rs.getBoolean(12));
                model.addRow(v);
            }
            conn.close();
            //rs.close();
        } catch (SQLException ex) {
            Logger.getLogger(Allowance.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Allowance.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Allowance.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Allowance.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Allowance.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Allowance().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTable tbBang;
    private javax.swing.JTextField txtAllowance;
    private javax.swing.JTextField txtEmployeeID;
    private javax.swing.JTextField txtTotalAllowance;
    private javax.swing.JTextField txtTransferID;
    // End of variables declaration//GEN-END:variables
}
